package com.xu.sso.client.api.utils;

import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class HttpUtil {

    private static Logger log = LoggerFactory.getLogger(HttpUtil.class);

    private static OkHttpClient client = null;

    private static OkHttpClient getInstance() {
        if (null == client) {
            synchronized (HttpUtil.class) {
                if (null == client) {
                    ConnectionPool pool = new ConnectionPool(20, 5, TimeUnit.MINUTES);
                    client = new OkHttpClient.Builder()
                            .connectionPool(pool)
                            .connectTimeout(10, TimeUnit.SECONDS)
                            .readTimeout(10, TimeUnit.SECONDS)
                            .build();
                }
            }
        }
        return client;
    }

    public static String get(String url) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            Response response = getInstance().newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                log.info(">>>>>>>>>>>>>>http response error code==={},mgs==={}", response.code(), response.message());
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>http error==={}", e);
        }
        return null;
    }

    public static String get(String url, Map<String, String> params) {
        try {
            StringBuilder sb = new StringBuilder(url);
            boolean first = true;
            for (String key : params.keySet()) {
                if (first) {
                    sb.append("?").append(key).append("=").append(params.get(key));
                    first = false;
                } else {
                    sb.append("&").append(key).append("=").append(params.get(key));
                }
            }
            Request request = new Request.Builder()
                    .url(sb.toString())
                    .build();
            Response response = getInstance().newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                log.info(">>>>>>>>>>>>>>http response error code==={},mgs==={}", response.code(), response.message());
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>http error==={}", e);
        }
        return null;
    }

    public static String postJson(String url, String json) {
        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            Response response = getInstance().newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                log.info(">>>>>>>>>>>>>>http response error code==={},mgs==={}", response.code(), response.message());
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>http error==={}", e);
        }
        return null;
    }

    public static String postXml(String url, String xml) {
        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/xml; charset=utf-8"), xml);
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            Response response = getInstance().newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                log.info(">>>>>>>>>>>>>>http response error code==={},mgs==={}", response.code(), response.message());
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>http error==={}", e);
        }
        return null;
    }

    public static String postForm(String url, Map<String, String> params) {
        try {
            FormBody.Builder builder = new FormBody.Builder();
            for (String key : params.keySet()) {
                builder.add(key, params.get(key));
            }
            Request request = new Request.Builder()
                    .url(url)
                    .post(builder.build())
                    .build();
            Response response = getInstance().newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                log.info(">>>>>>>>>>>>>>http response error code==={},mgs==={}", response.code(), response.message());
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>http error==={}", e);
        }
        return null;
    }

}
